package com.hwc.demo.mbs.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Example;
import org.springframework.data.domain.ExampleMatcher;
import org.springframework.data.domain.ExampleMatcher.StringMatcher;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.PageRequest;
import org.springframework.data.domain.Pageable;
import org.springframework.data.domain.Sort;
import org.springframework.data.domain.Sort.Order;
import org.springframework.stereotype.Service;

import com.hwc.demo.mbs.bean.entity.Student;
import com.hwc.demo.mbs.repository.StudentRepository;

@Service
public class StudentService {
	@Autowired
	StudentRepository stuRepo;
	
	/**
	 * JPA 综合条件分页查询
	 * @param page
	 * @param size
	 * @param entity
	 * @return
	 */
	public Page<Student> findPage(int page, int size, Student entity){
		//分页+属性排序
		Pageable pageable = PageRequest.of(page, size, Sort.by(Order.desc("id"))); 
		//字符串包含查（即左右模糊查）+ 忽略大小写 + non-null空串支持
		Example<Student> example = Example.of(entity, ExampleMatcher.matching().withStringMatcher(StringMatcher.CONTAINING));
		//查
		Page<Student> pager = stuRepo.findAll(example, pageable);
		
		return pager;
	}
	
	public Student merge(Student entity) {
		
		return stuRepo.saveAndFlush(entity);
	}
}
